package com.eagle.security.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import com.eagle.security.dao.GroupDao;
import com.eagle.security.entries.RPConstant;
import com.eagle.security.entries.RoleGroup;
import com.eagle.security.service.RoleGroupManageService;

@Service
public class RoleGroupManageServiceImpl implements RoleGroupManageService {

    @Resource(name="userSecurityDao")
    private GroupDao roleGroupDao;

    @Override
    public int saveRoleGroup(RoleGroup mo) {
//        boolean b = roleGroupDao.roleGroupExists( mo.getGroupname());
//        if( b ){    //用户已经存在
//            return 0 ;
//        }
        roleGroupDao.createGroup(mo.getGroupname(), null );
        return  1;
    }

    @Override
    @PreAuthorize("hasAnyRole('"+RPConstant.ROLEGROUP_MANAGER+","+RPConstant.ROLEGROUP_EDIT+"')")
    public int modifyRoleGroup(RoleGroup mo) {
        return roleGroupDao.modifyRoleGroup( mo );
    }

    @Override
    public int deleteRoleGroupByName (String roleGroupname) {
        roleGroupDao.deleteGroup(roleGroupname);
        return 1;
    }

    @Override
    public RoleGroup findRoleGroupByName(String roleGroupname) {
        return roleGroupDao.findRoleGroupByName( roleGroupname );
    }

    @Override
    public Map<String, Object> findRoleGroupMapByName(String name ) {
        return roleGroupDao.findRoleGroupMapByName( name );
    }

    @Override
    public List<RoleGroup> findRoleGroupsByCond(RoleGroup mo, int page, int pageSize) {
        int start = (page - 1) * pageSize;
        return roleGroupDao.findRoleGroupsByCond(mo, start, pageSize);
    }

    @Override
    public List<Map<String, Object>> findRoleGroupMapsByCond(RoleGroup mo, int page, int pageSize) {
        int start = (page - 1) * pageSize;
        return roleGroupDao.findRoleGroupMapsByCond(mo, start, pageSize);
    }

    @Override
    public int findRoleGroupCountByCond(RoleGroup mo) {
        return roleGroupDao.findRoleGroupCountByCond(mo);
    }

    @Override
    public int checkExistWhenSaveRoleGroup(RoleGroup mo) {
//        boolean b = roleGroupDao.roleGroupExists(mo.getGroupname());
//        return b == true ? 1 : 0 ; 
        return 1 ;
    }

    @Override
    public int assignRolegroupPermission(String rolegroup_id, String[] permission_keys){
    	roleGroupDao.removeGroupAuthoritysById(rolegroup_id);
    	List<GrantedAuthority> authoritys = new ArrayList<GrantedAuthority>();
    	for (String key : permission_keys) {
    		GrantedAuthority au = new SimpleGrantedAuthority(key);
    		authoritys.add(au);
		}
    	return roleGroupDao.addGroupAuthoritys(rolegroup_id, authoritys );
    }

	@Override
	public String findRolegroupPermission(String groupId) {
		List<GrantedAuthority> aus = roleGroupDao.findGroupAuthoritiesById(groupId);
		StringBuffer b = new StringBuffer();
		for (GrantedAuthority au : aus) {
			b.append(au.getAuthority()).append(",");
		}
		return b.toString();
	}

	@Override
	public void addUserToGroupById(String username, String groupId ) {
        if( StringUtils.isBlank(username) || StringUtils.isBlank(groupId) ){
        	return ;
        }
        roleGroupDao.removeUserFromGroupById(username, groupId);
		roleGroupDao.addUserToGroupById(username, groupId);
	}
    
	@Override
	public void removeUserFromAllGroup(String username){
		roleGroupDao.removeUserFromAllGroup(username);
	}
}
